package com.atguigu.gulimall.product.controller;


import com.atguigu.gulimall.product.entity.Category;
import com.atguigu.gulimall.product.service.impl.CategoryServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.atguigu.common.utils.R;

import java.util.Arrays;
import java.util.List;

/**
 * <p>
 * 商品三级分类 前端控制器
 * </p>
 *
 * @author szh
 * @since 2020-12-16
 */
@RestController
@RequestMapping("/product/category")
public class CategoryController {
	@Autowired
	private CategoryServiceImpl categoryService;

	@GetMapping("/list/tree")
	public R list(){
		List<Category> categoryList =categoryService.listWithTree();
		return R.ok().put("data",categoryList);
	}

	@PostMapping("/delete")
	public R removeMenuByIds(@RequestBody List<Long> ids){
		int delFlag = categoryService.removeMenuByIds(ids);
		return R.ok().put("code",delFlag);
	}
	@PostMapping("/save")
	public R saveCategory(@RequestBody Category category){
		boolean saveFlag = categoryService.save(category);
		return R.ok().put("saveFlag",saveFlag);
	}
	/**
	 * 功能 根据主键查询一条记录
	 * @param catId  主键id
	 */
	@RequestMapping("/info/{catId}")
	public R info(@PathVariable("catId") Long catId){
		Category category = categoryService.getById(catId);

		return R.ok().put("data", category);
	}
	/**
	 * 功能：根据主键id，修改某条记录
	 * @param category  实体（注意这个实体只有要修改的字段）
	 */
	@RequestMapping("/update")
	public R update(@RequestBody Category category){
		categoryService.updateCascade(category);
		return R.ok();
	}
	/**
	* 功能：批量修改排序
	* */
	@RequestMapping("/update/sort")
	public R updateSort(@RequestBody Category[] category){
		categoryService.updateBatchById(Arrays.asList(category));
		return R.ok();
	}
	/**
	 * 批量删除
	 *
	 * SpringMVC自动将请求体的数据（json），转为对应的对象
	 */
	@RequestMapping("/delete")
	public R delete(@RequestBody Long[] catIds){
		categoryService.removeMenuByIds(Arrays.asList(catIds));
		return R.ok();
	}
}
